package com.smx.mvc;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @ClassName HandlerMapping
 * @Description TODO
 * @Author moses
 * @Date 2021/1/24
 **/
public class HandlerMapping {
    private static Map<String, MVCMapping> data = new HashMap<>();

    public static MVCMapping get(String uri) {
        return data.get(uri);
    }

    public static void load(InputStream is) {
        Properties ppt = new Properties();
        try {
            ppt.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //获取值的集合
        Collection<Object> values = ppt.values();
        for (Object value : values) {
            String className = (String) value;
            try {
                //加载每一个类
                Class c = Class.forName(className);
                //创建这个类的对象
                Object obj = c.getConstructor().newInstance();
                //获取这个类的所有方法
                Method[] methods = c.getMethods();
                for (Method method : methods) {
                    Annotation[] as = method.getAnnotations();
                    if (as != null) {
                        for (Annotation annotation : as) {
                            if (annotation instanceof ResponseBody) {
                                //说明此方法返回字符串
                                MVCMapping mapping = new MVCMapping(obj, method, ResponseType.TEXT);
                                data.put(((ResponseBody) annotation).value(), mapping);
                            } else if (annotation instanceof ResponseView) {
                                //说明此方法返回界面
                                MVCMapping mapping = new MVCMapping(obj, method, ResponseType.VIEW);
                                data.put(((ResponseView) annotation).value(), mapping);
                            }
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static class MVCMapping {
        private Object obj;
        private Method method;
        private ResponseType type;

        public MVCMapping() {
        }

        public MVCMapping(Object obj, Method method, ResponseType type) {
            this.obj = obj;
            this.method = method;
            this.type = type;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }

        public ResponseType getType() {
            return type;
        }

        public void setType(ResponseType type) {
            this.type = type;
        }
    }

}
